package cn.gz.ibix.ibix_library;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import cn.gz.ibix.ibix_library.account.adapter.AccountAdapter;
import cn.gz.ibix.ibix_library.account.loader.AccountLoader;
import cn.gz.ibix.library.loader.Listener;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, Listener {

    private ListView listview;
    private Button bt;
    private AccountAdapter accountAdapter = new AccountAdapter();

    private AccountLoader accountLoader = AccountLoader.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        accountLoader.setListener(this);
    }

    private void initView() {
        listview = (ListView) findViewById(R.id.listview);
        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(this);
        listview.setAdapter(accountAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt:
                accountLoader.load();
                break;
        }
    }

    @Override
    public void onSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                accountAdapter.getList().clear();
                accountAdapter.getList().addAll(accountLoader.getList());
                accountAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onFail(Object object) {

    }

    @Override
    public void onError(Exception e) {

    }

}
